package ll1;

import java.util.ArrayList;

/**
 * Table elements of LL(1) parse table
 * @author Peter
 *
 */
public class LL1Node {
	//Example rule: <Something> -> <One> | a | epsilon
	//Name: Something
	//Content: [<One>, |, a, |, epsilon]
	private String name;				//The name of this terminal/non-terminal entry
	private ArrayList<Token> content;	//The things that could be generated from this rule (contains both terminal and nonterminal)
	
	//Table information
	private String row;
	private String col;
	private ArrayList<LL1Node> others;
	
	public LL1Node(){
		content = new ArrayList<Token>();
	}
	
	public LL1Node(String aName){
		setName(aName);
		content = new ArrayList<Token>();
	}
	
	/**
	 * Constructor for LL1 parse table HEAD node
	 * @return
	 */
	public LL1Node(String aName, LL1Node rule){
		setName(aName);
		setOthers(new ArrayList<LL1Node>());
		others.add(rule);
	}
	
	/**
	 * @param row the row to set
	 */
	public void setRow(String row) {
		this.row = row;
	}
	/**
	 * @return the row
	 */
	public String getRow() {
		return row;
	}
	/**
	 * @param col the col to set
	 */
	public void setCol(String col) {
		this.col = col;
	}
	/**
	 * @return the col
	 */
	public String getCol() {
		return col;
	}
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public ArrayList<Token> getContent() {
		return content;
	}

	public void setContent(ArrayList<Token> content) {
		this.content = content;
	}
	/**
	 * @param others the others to set
	 */
	public void setOthers(ArrayList<LL1Node> others) {
		this.others = others;
	}
	/**
	 * @return the others
	 */
	public ArrayList<LL1Node> getOthers() {
		return others;
	}
	
	public String printRule(){
		String rule = getName() + " -> ";
		for(Token token : content){
			if(!token.isTerminal()){
				//rule += "<" + token.getName() + "> ";
				rule += token.getName() + " ";
			}
			else{
				rule += token.getName() + " ";
			}
		}
		return rule;
	}
	
	public String toString(){
		return printRule();
		//return getName();
	}

	public int getOrSymbols() {
		int orSymbols = 0;
		for(Token token : content){
			if(token.getName().equals("|")){
				orSymbols++;
			}
		}
		return orSymbols;
	}
}
